package bsc.war.handler;

import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.ejb.entity.ScoreView;
import bsc.ejb.entity.Topic;
import bsc.ejb.entity.TopicScoreView;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import bsc.war.service.ScoreCardUtils;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfTable;
import java.net.URL;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Memo
 */
public class SRMMemo extends AbstractController {

    public SRMMemo() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idSRM = request.getParameter("srm") != null && NumberUtils.isNumber(request.getParameter("srm")) ? Long.parseLong(request.getParameter("srm")) : null;
            /*Long idScoreView = request.getParameter("scoreView") != null && NumberUtils.isNumber(request.getParameter("scoreView")) ? Long.parseLong(request.getParameter("scoreView")) : null;*/
            SRM srm = delegate.findSRM(idSRM);
            ScoreView scoreView = srm.getScoreView();
            List<Topic> srmTopics = delegate.findTopic(srm);

            response.setContentType("application/pdf");
            Document document = new Document(PageSize.A4.rotate());
            document.setMargins(10, 10, 10, 10);

            Font titleFont = new Font();
            titleFont.setFamily("Arial");
            titleFont.setSize(20);
            titleFont.setStyle(Font.BOLD);

            Font headerFont = new Font();
            headerFont.setFamily("Arial");
            headerFont.setSize(10);
            headerFont.setStyle(Font.BOLD);

            Font bodyFont = new Font();
            bodyFont.setFamily("Arial");
            bodyFont.setSize(10);

            PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            document.addTitle(srm.getName());
            
            Image image = Image.getInstance(new URL(BSCUtils.getServerPath(request)+((Framework)request.getSession(true).getAttribute("framework")).getLogo()));
            image.setAlignment(Image.ALIGN_RIGHT);
            image.setIndentationRight(20);
            image.scalePercent(50f, 50f);
            document.add(image);

            Paragraph memo = new Paragraph("MINUTA\n",titleFont);
            memo.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(memo);

            Paragraph title = new Paragraph(srm.getName()+"\n\n\n");
            title.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(title);
            
            PdfPTable resume = new PdfPTable(4);
            resume.setWidthPercentage(95);
            PdfPCell resumeHeader = new PdfPCell();
            resumeHeader.setBackgroundColor(new BaseColor(159, 159, 204));
            resumeHeader.setPhrase(new Phrase("Lugar",headerFont));
            resume.addCell(resumeHeader);
            resume.addCell(new Phrase(srm.getPlace() != null ? srm.getPlace() : "No especificado",bodyFont));
            resumeHeader.setPhrase(new Phrase("Fecha y Hora",headerFont));
            resume.addCell(resumeHeader);
            if( srm.getSRMDate()==null && srm.getSRMHour()==null )
                resume.addCell(new Phrase("No especificadas",bodyFont));
            else
                resume.addCell( new Phrase((srm.getSRMDate() != null ? srm.getSRMDate() + " " : "" )
                    +(srm.getSRMHour() != null ? srm.getSRMHour() : ""),bodyFont));
            resumeHeader.setPhrase(new Phrase("Responsable",headerFont));
            resume.addCell(resumeHeader);
            resume.addCell(new Phrase(srm.getResponsable() != null ? srm.getResponsable() : "No especificado",bodyFont));
            resumeHeader.setPhrase(new Phrase("Tiempo Total",headerFont));
            resume.addCell(resumeHeader);
            resume.addCell(new Phrase(srm.getExpectedTime() != null ? ""+srm.getExpectedTime() + "hrs" : "No especificado",bodyFont));
            document.add(resume);

            // TEMAS A TRATAR
            document.add(new Paragraph("\n\n\n"));
            PdfPTable topics = new PdfPTable(5);
            topics.setWidthPercentage(95);
            topics.setHorizontalAlignment(PdfTable.ALIGN_CENTER);
            
            PdfPCell header = new PdfPCell();
            resumeHeader.setHorizontalAlignment(PdfTable.ALIGN_CENTER);
            header.setColspan(5);
            Paragraph tableHeader = new Paragraph("Temas a tratar",headerFont);
            tableHeader.setAlignment(Paragraph.ALIGN_CENTER);
            header.setPhrase(tableHeader);
            header.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            header.setBackgroundColor(new BaseColor(159, 159, 204));
            topics.addCell(header);
            resumeHeader.setBackgroundColor(new BaseColor(208, 208, 223));
            resumeHeader.setPhrase(new Phrase("Tema",headerFont));
            topics.addCell(resumeHeader);
            resumeHeader.setPhrase(new Phrase("Acuerdo",headerFont));
            topics.addCell(resumeHeader);
            resumeHeader.setPhrase(new Phrase("Responsable del acuerdo",headerFont));
            topics.addCell(resumeHeader);
            resumeHeader.setPhrase(new Phrase("Estatus",headerFont));
            topics.addCell(resumeHeader);
            resumeHeader.setPhrase(new Phrase("Avance",headerFont));
            topics.addCell(resumeHeader);

            for (Topic topic : srmTopics) {
                SRMTopic srmTopic = delegate.findSRMTopic(srm, topic);
                TopicScoreView topicScoreView = delegate.findTopicScoreView(topic, scoreView);
                topics.addCell(new Phrase(srmTopic.getTopic().getTopic(),bodyFont));
                topics.addCell(new Phrase(srmTopic.getDeal(),bodyFont));
                topics.addCell(new Phrase(srmTopic.getResponsable(),bodyFont));
                topics.addCell(new Phrase(ScoreCardUtils.topicStatus(topicScoreView.getType()),bodyFont));
                topics.addCell(new Phrase(srmTopic.getProgress() != null ? srmTopic.getProgress().toString() : "No especificado",bodyFont));
            }
            document.add(topics);
            document.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
